//
//  VoiceReadItemFooterView.m
//  ShowYork
//
//  Created by hxh on 2021/5/17.
//  Copyright © 2021 hxh. All rights reserved.
//

#import "VoiceReadItemFooterView.h"
#import "PublicAudioPlayProgressView.h"
#import "ZFSliderView.h"



@interface VoiceReadItemFooterView ()<ZFSliderViewDelegate>
@property (nonatomic,strong)UILabel *currentTimeLb;
@property (nonatomic,strong)UILabel *totalTimeLb;
@property (nonatomic,strong)UIImageView *lastImgView;
@property (nonatomic,strong)UIImageView *nextImgView;
@property (nonatomic,strong)UIImageView *playImgView;
@property (nonatomic,strong)UIImageView *listImgView;
@property (nonatomic,strong)ZFSliderView *sliderView;
@property (nonatomic,strong)UIButton *playBtn;
@property (nonatomic,strong)CAGradientLayer *gradientLayer;
@property (nonatomic,strong)UIActivityIndicatorView *spin;


@property (nonatomic,strong)UIView *containView;
@property (nonatomic,strong)PublicAudioPlayProgressView *progressView;
@end

@implementation VoiceReadItemFooterView

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self setUI];
    }
    return self;
}
- (void)setUI{
    
    self.frame = CGRectMake(0, 0, kScreenW, GK_SAVEAREA_BTM + 120 + 12);
    
    self.containView = [[UIView alloc]init];
    self.containView.backgroundColor = [UIColor whiteColor];
    [self addSubview:self.containView];
    
    [self.containView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.left.right.equalTo(self);
        make.height.mas_equalTo(GK_SAVEAREA_BTM + 120);
    }];
    
    self.playBtn = [[UIButton alloc]init];
    [self.playBtn addTarget:self action:@selector(playMethod) forControlEvents:UIControlEventTouchUpInside];
    self.playBtn.backgroundColor = RGBA(238, 157, 59, 1);
    self.playBtn.layer.cornerRadius = 22;
    self.playBtn.layer.masksToBounds = YES;
    [self addSubview:self.playBtn];

    [self.playBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(74, 44));
        make.bottom.equalTo(self).offset(-(GK_SAVEAREA_BTM + 20));
        make.centerX.equalTo(self);
    }];
    
    self.lastImgView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"voice_last_tag"]];
    [self addSubview:self.lastImgView];
    
    self.nextImgView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"voice_next_tag"]];
    [self addSubview:self.nextImgView];
    
    [self.lastImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.playBtn);
        make.right.equalTo(self.playBtn.mas_left).offset(-32);
    }];
    
    [self.nextImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.playBtn);
        make.left.equalTo(self.playBtn.mas_right).offset(32);
    }];
    
    self.listImgView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"voice_list_tag"]];
    [self addSubview:self.listImgView];
    [self.listImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self).offset(-28);
        make.centerY.equalTo(self.playBtn);
    }];
    
    
    self.playImgView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"voice_pause_tag"]];
    self.playImgView.highlightedImage = [UIImage imageNamed:@"voice_play_tag"];
    [self addSubview:self.playImgView];
    [self.playImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self.playBtn);
    }];
    
    self.sliderView = [[ZFSliderView alloc] init];
    self.sliderView.thumbSize = CGSizeMake(10, 10);
    self.sliderView.animate = NO;
    self.sliderView.allowTapped = NO;
    self.sliderView.delegate = self;
    self.sliderView.minimumTrackTintColor = RGBA(238, 157, 59, 1);
    self.sliderView.height = 2;
    [self.sliderView startForward];
    
    self.sliderView.sliderBtn.backgroundColor = RGBA(238, 157, 59, 1);
    

    [self addSubview:self.sliderView];
    [self.sliderView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(36);
        make.top.equalTo(self).offset(12);
        make.left.equalTo(self).offset(50);
        make.right.equalTo(self).offset(-50);
    }];
    
    self.currentTimeLb = [[UILabel alloc]init];
    self.currentTimeLb.textColor = C51;
    self.currentTimeLb.font = font(10);
    [self addSubview:self.currentTimeLb];
    [self.currentTimeLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.sliderView);
        make.right.equalTo(self.sliderView.mas_left).offset(-10);
    }];
    
    self.totalTimeLb = [[UILabel alloc]init];
    self.totalTimeLb.textColor = C51;
    self.totalTimeLb.font = font(10);
    [self addSubview:self.totalTimeLb];
    [self.totalTimeLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.sliderView);
        make.left.equalTo(self.sliderView.mas_right).offset(8);
    }];
    
    self.gradientLayer = [CAGradientLayer layer];
    self.gradientLayer.frame = CGRectMake(0, 0, kScreenW, 12);
    self.gradientLayer.colors = [NSArray arrayWithObjects:
    (id)[RGBA(255, 255, 255, 0.0) CGColor],
    (id)[RGBA(255, 255, 255, 1) CGColor],nil];
    [self.layer addSublayer:self.gradientLayer];
    
    
    self.spin = [[UIActivityIndicatorView alloc] init];
    self.spin.color = [UIColor whiteColor];
    self.spin.hidesWhenStopped = YES;
    [self addSubview:self.spin];
    
    [self.spin mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self.playBtn);
        make.size.mas_equalTo(CGSizeMake(40, 40));
    }];
    
    self.listImgView.userInteractionEnabled = YES;
    UITapGestureRecognizer *listTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(listMethod)];
    [self.listImgView addGestureRecognizer:listTap];
    
    self.nextImgView.userInteractionEnabled = YES;
    UITapGestureRecognizer *nextTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(nextMethod)];
    [self.nextImgView addGestureRecognizer:nextTap];
    
    self.lastImgView.userInteractionEnabled = YES;
    UITapGestureRecognizer *lastTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(lastMethod)];
    [self.lastImgView addGestureRecognizer:lastTap];
    
}
  
- (void)listMethod{
    if (self.delegate && [self.delegate respondsToSelector:@selector(showAllVoice)]) {
        [self.delegate showAllVoice];
    }
}

- (void)lastMethod{
    if (self.delegate && [self.delegate respondsToSelector:@selector(lastPlay)]) {
        [self.delegate lastPlay];
    }
}

- (void)nextMethod{
    if (self.delegate && [self.delegate respondsToSelector:@selector(nextPlay)]) {
        [self.delegate nextPlay];
    }
}

- (void)playMethod{
    

    if (self.itemModel.status == XHPlayerListItemNormal || self.itemModel.status == XHPlayerListItemFailed) {
        // 判断是第一次加载
        if (self.delegate && [self.delegate respondsToSelector:@selector(startPlay)]) {
            [self.delegate startPlay];
            
            if (![self.spin isAnimating]) {
                [self.spin startAnimating];
            }
            self.playImgView.hidden = YES;
            self.playBtn.userInteractionEnabled = NO;
        }
    }
    if (self.itemModel.status == XHPlayerListItemPlay) {
        if (self.delegate && [self.delegate respondsToSelector:@selector(pausePlay)]) {
            [self.delegate pausePlay];
            self.playImgView.highlighted = NO;
        }
    }
    if (self.itemModel.status ==XHPlayerListItemPause) {
        if (self.delegate && [self.delegate respondsToSelector:@selector(continuePlay)]) {
            [self.delegate continuePlay];
            self.playImgView.highlighted = YES;
        }
    }
}

// 滑块滑动开始
- (void)sliderTouchBegan:(float)value{
    self.sliderView.thumbSize = CGSizeMake(14, 14);
    if (self.delegate && [self.delegate respondsToSelector:@selector(sliderBegan)]) {
        [self.delegate sliderBegan];
    }
}
// 滑块滑动中
- (void)sliderValueChanged:(float)value{
    
    int currentCount = self.itemModel.totalTime *value;
    NSString *currentTime = [NSString stringWithFormat:@"%02d:%02d",currentCount/60,currentCount%60];
    self.currentTimeLb.text = currentTime;
}
// 滑块滑动结束
- (void)sliderTouchEnded:(float)value{
    self.sliderView.thumbSize = CGSizeMake(10, 10);
    if (self.delegate && [self.delegate respondsToSelector:@selector(sliderEnded:)]) {
        [self.delegate sliderEnded:value];
    }
}
// 滑杆点击
- (void)sliderTapped:(float)value{

}

- (void)setItemModel:(VoiceReadPlayerItemModel *)itemModel{
    
    _itemModel = itemModel;
    
    if (itemModel.status == XHPlayerListItemPlay) {
        if ([self.spin isAnimating]) {
            [self.spin stopAnimating];
        }
        self.playImgView.hidden = NO;
        self.playImgView.highlighted = YES;
    }else if (itemModel.status == XHPlayerListItemFailed){
        if ([self.spin isAnimating]) {
            [self.spin stopAnimating];
        }
        self.playImgView.hidden = NO;
        self.playImgView.highlighted = NO;
    }else if (itemModel.status == XHPlayerListItemReady){
        if (![self.spin isAnimating]) {
            [self.spin startAnimating];
        }
        self.playImgView.hidden = YES;
    }else if (itemModel.status == XHPlayerListItemPause){
        self.playImgView.hidden = NO;
        self.playImgView.highlighted = NO;
    }
    self.playBtn.userInteractionEnabled = YES;
    
    int totalCount = itemModel.voiceTime.intValue/1000;
    NSString *totalTime = [NSString stringWithFormat:@"%02d:%02d",totalCount/60,totalCount%60];
    self.totalTimeLb.text = totalTime;
    
    int currentCount = itemModel.currentTime;
    NSString *currentTime = [NSString stringWithFormat:@"%02d:%02d",currentCount/60,currentCount%60];
    self.currentTimeLb.text = currentTime;
    
    if (totalCount == 0) {
        [self.sliderView setValue:0];
    }else{
        CGFloat value = (CGFloat)currentCount/(CGFloat)totalCount;
        [self.sliderView setValue:value];
    }
    
  
}


@end
